package com.example.remindme.RemindModule.DataCard.Fragment

import androidx.lifecycle.*
import com.example.remindme.RemindDatabase.*
import com.example.remindme.RemindModule.DataCard.DataBase.MatterDatabase
import com.example.remindme.RemindModule.DataCard.ViewHolder.ClickData
import com.example.remindme.RemindModule.DataCard.ViewHolder.MyList
import com.example.remindme.RemindModule.DataCard.ViewHolder.RecyclerData
import kotlinx.coroutines.coroutineScope
import java.util.*


class ViewcardViewModel(application: android.app.Application) : AndroidViewModel(application) {
    var currentType=MutableLiveData<String>()
    lateinit var current: ClickData
    var listNum:Int?=0
   //  var listData= arrayListOf<RecyclerData>()
    var listNameData= arrayListOf<RecyclerData>()
    var listMatterData=arrayListOf<RecyclerData>()
    var finishRoom=MutableLiveData<Boolean>()
    var allNum=0
    var scheduledNum=0
    var todayNum=0
    var flagNum=0


    // 为每个list，都初始化为未被点击过
     val matterDatabase by lazy {
        MatterDatabase.getDatabase(getApplication())  // 数据库在实例化的时候需要。。Context. 数据库只需要一个就行了
    }
    lateinit var matterDao: MatterDao
    lateinit var listDao: ListDao

  suspend fun requeryRoom(){

  //    listData.clear()
      listNameData.clear()
      val cal=Calendar.getInstance()
// 设置为查询到的列表数目，变化的时候，自动更新ui
      // 显示的data，总有类型名吧
       coroutineScope{
           val tem1=listDao.getAll()
           listNum=tem1.size
           tem1.forEach{
               val tt=MyList(it.type)
               listNameData.add(tt)
           }
           allNum=matterDao.getAllNum()
           scheduledNum= matterDao.getScheduledNum()
           flagNum=matterDao.getFlagNum()
          val temp=matterDao.getMatterTimed()
           temp.value?.forEach {
               val date= Date(it.timed)
               val dataCal= Calendar.getInstance()
               dataCal.time=date
               // 判断存储的时间是不是一天
               if(cal.get(Calendar.DAY_OF_YEAR)==dataCal.get(Calendar.DAY_OF_YEAR)
                       && cal.get(Calendar.YEAR)==dataCal.get(Calendar.YEAR))
               {
                  todayNum++
               }

           }

           finishRoom.postValue(true)  // 完成查询
       }
       // clicked recipe channel project
        // 数据库查询9
    }
  suspend fun addListType(entity: ListEntity)
    {
      coroutineScope {
          listDao.insertRegister(entity)
      }
    }
    suspend fun addMatter(entity: MatterEntity)
    {
        coroutineScope {
            matterDao.insertRegister(entity)
        }

    }



}
